package com.yzs.core.ext

import android.os.Parcelable
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.SizeUtils

/**
 * author : yzs
 * time   : 2021/06/30
 * desc   : view 相关的扩展
 */


/**
 * 防止重复点击,可同时注册多个view
 */
fun setNoRepeatClick(vararg views: View, interval: Long = 300, onClick: (View) -> Unit) {
    views.forEach {
        it.clickNoRepeat(interval = interval) { view ->
            onClick.invoke(view)
        }
    }
}

/**
 * 防止重复点击
 * @param interval 重复间隔
 * @param onClick  事件响应
 */
var lastTime = 0L
fun View.clickNoRepeat(interval: Long = 400, onClick: (View) -> Unit) {
    setOnClickListener {
        val currentTime = System.currentTimeMillis()
        if (lastTime != 0L && (currentTime - lastTime < interval)) {
            return@setOnClickListener
        }
        lastTime = currentTime
        onClick(it)
    }
}


fun View.setVisible(visible: Boolean): View {
    this.visibility = if (visible) View.VISIBLE else View.INVISIBLE
    return this
}

fun View.setGone(gone: Boolean): View {
    this.visibility = if (gone) View.GONE else View.VISIBLE
    return this
}

fun View.isVisible() = visibility == View.VISIBLE
fun View.isGone() = visibility == View.GONE

fun View.measuredHeight(): Int = SizeUtils.getMeasuredHeight(this)
fun View.measuredWidth(): Int = SizeUtils.getMeasuredWidth(this)


/**
 * viewPager适配fragment
 */
fun ViewPager2.initFragment(
    activity: AppCompatActivity, fragments: MutableList<Fragment>
): ViewPager2 {
    //设置适配器
    adapter = object : FragmentStateAdapter(activity) {
        override fun createFragment(position: Int) = fragments[position]
        override fun getItemCount() = fragments.size
    }
    return this
}


/**
 * viewPager2适配fragment
 */
fun ViewPager2.initFragment(
    fragment: Fragment, fragments: MutableList<Fragment>
): ViewPager2 {
    //设置适配器
    adapter = object : FragmentStateAdapter(fragment) {
        override fun createFragment(position: Int) = fragments[position]
        override fun getItemCount() = fragments.size
    }
    return this
}


/**
 * ViewPager于fragment绑定
 * 通过BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT支持懒加载
 */
fun ViewPager.initFragment(
    manager: FragmentManager, fragments: MutableList<Fragment>
): ViewPager {
    //设置适配器
    adapter = object : FragmentStatePagerAdapter(
        manager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
    ) {
        override fun getCount() = fragments.size

        override fun getItem(position: Int): Fragment {
            return fragments[position]
        }

        override fun saveState(): Parcelable? {
            return null
        }
    }
    return this
}

/**
 * ViewPager和View绑定
 */
fun ViewPager.initView(
    views: MutableList<View>
): ViewPager {
    adapter = object : PagerAdapter() {
        override fun getCount() = views.size
        override fun isViewFromObject(view: View, any: Any) = view == any
        override fun destroyItem(container: ViewGroup, position: Int, any: Any) {
            container.removeView(views[position])
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val view = views[position]
            container.addView(view)
            return view
        }

        override fun getPageTitle(position: Int) = views[position].tag.toString()
    }
    return this
}

